package org.jash.demo02

import android.annotation.SuppressLint
import android.os.Bundle
import android.util.Log
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.Image
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.aspectRatio
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.GridItemSpan
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.foundation.lazy.grid.items
import androidx.compose.foundation.lazy.staggeredgrid.LazyVerticalStaggeredGrid
import androidx.compose.foundation.lazy.staggeredgrid.StaggeredGridCells
import androidx.compose.foundation.lazy.staggeredgrid.items
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Call
import androidx.compose.material3.Card
import androidx.compose.material3.Scaffold
import androidx.compose.material3.SnackbarHost
import androidx.compose.material3.SnackbarHostState
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.internal.composableLambda
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.painter.Painter
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.navigation.NavController
import androidx.navigation.NavHostController
import androidx.navigation.compose.rememberNavController
import coil3.compose.AsyncImage
import kotlinx.coroutines.launch
import kotlinx.serialization.Serializable
import org.jash.demo02.entity.Goods
import org.jash.demo02.ui.theme.Demo02Theme
import org.jash.demo02.utils.ApiServer
import org.jash.demo02.utils.retrofit
import androidx.navigation.compose.composable
import androidx.navigation.compose.NavHost
import org.jash.demo02.ui.P01
import org.jash.demo02.ui.P02
import org.jash.demo02.ui.Page01
import org.jash.demo02.ui.Page02

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContent {
            Demo02Theme {
                Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
                    MyApp(
                        modifier = Modifier.padding(innerPadding)
                    )
                }
            }
        }
    }
}
@Composable
fun MyApp(modifier: Modifier) {
    val controller: NavHostController = rememberNavController()
    NavHost(navController = controller, startDestination = Main) {
        composable<Main> { Greeting("", modifier, controller) }
        composable<P01> { Page01(modifier, controller) }
        composable<P02> { Page02(modifier, controller) }
    }
}
@Serializable
data object Main
@SuppressLint("CoroutineCreationDuringComposition", "UnusedMaterial3ScaffoldPaddingParameter")
@Composable
fun Greeting(name: String, modifier: Modifier = Modifier, controller: NavController) {
    val scope = rememberCoroutineScope()
    val server = retrofit.create(ApiServer::class.java)
    var goods: MutableList<Goods> = remember { mutableStateListOf() }
    val snackbar: SnackbarHostState = remember { SnackbarHostState() }
    scope.launch {
        val res = server.getGoods(0, 1, 100)
        if (res.code == 200) {
            goods.addAll(res.data)
//            res.data.forEach {
//                Log.d("TAG", "Greeting: $it")
//            }
        }
    }
    Scaffold(
        modifier = modifier,
        snackbarHost = { SnackbarHost(snackbar) }
    ) {
        LazyVerticalGrid (columns = GridCells.Adaptive(minSize = 128.dp)) {
            item(span = { GridItemSpan(maxLineSpan) }) {
                Image(
                    painter = painterResource(id = R.drawable.ic_launcher_background),
                    contentDescription = null,
                    modifier = Modifier
                        .clickable(true) {
                            controller.navigate(P01)
                        }
                )
            }
            items(goods) {
                Card(
                    modifier = Modifier.padding(5.dp),
                    onClick = {
                        Log.d("TAG", "Greeting: ${it.goodsDesc}")
                        scope.launch { snackbar.showSnackbar(it.goodsDesc) }
                    }
                ) {
                    Column {
                        AsyncImage(
                            model = it.goodsIcon,
                            contentDescription = null,
                            contentScale = ContentScale.FillWidth,
                            modifier = Modifier
                                .fillMaxWidth()
                                .aspectRatio(1f)
                        )
                        Text(
                            text = it.goodsDesc,
                            maxLines = 2,
                            overflow = TextOverflow.Ellipsis
                        )
                    }
                }
            }
        }
    }

//    LazyColumn(modifier = modifier) {
//        items(goods) {
//            Row {
//                AsyncImage(
//                    model = it.goodsIcon,
//                    contentDescription = null,
//                    modifier = Modifier
//                        .width(100.dp)
//                        .height(100.dp)
//                )
//                Text(it.goodsDesc)
//            }
//        }
//    }
//    Column(modifier = modifier) {
//        Text(
//            text = "你好, ${name}"
//        )
//        Text(text = "第二行文本")
//        Text(text = "第二行文本")
//        Image(
//            painter = painterResource(id = R.drawable.ic_launcher_background),
//            contentDescription = ""
//        )
//        AsyncImage(
//            model = "https://profile-avatar.csdnimg.cn/935ee80279a84935bcade6d31114929c_geyunfei_.jpg!1",
//            contentDescription = ""
//        )
//    }
}

@Preview(showBackground = true, widthDp = 720, heightDp = 1080)
@Composable
fun GreetingPreview() {
    Demo02Theme {
//        Greeting()
    }
}